<style>
    img.fans-portrait{
        width:50px;
        height:50px;
    }
    .fsh-rightPanel .layui-table-body .layui-table-cell {
        height: 50px;
        line-height: 50px;
    }
</style>
<div class="fsh-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item wx-search" id="list_form">
            <div class="layui-inline">
                <input type="text" name="roleName" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search">搜索</button>
            <div class="layui-btn-container" style="text-align: right">
                  <button class="layui-btn btn-primary" id="role_add">新建角色</button>
            </div>
        </div>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>

<script>

    function getDateStr(v) {
        var date = new Date(v);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? ("0" + d) : d;
        var h = date.getHours();
        h = h < 10 ? ("0" + h) : h;
        var M = date.getMinutes();
        M = M < 10 ? ("0" + M) : M;
        var str = y + "-" + m + "-" + d + " " + h + ":" + M;
        return str;
    }

    var parentId;
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;


        var tableObj = table.render({
            elem: '#list_table',
            url: context+'/api/sysrole/list',
            align: "center",
            cols: [[ //表头
                {field: 'roleName', title: '名称', align: 'center'},
                {field: 'roleKey', title: '别名',  align: 'center'},
                {field: 'parentId', title: '父级ID',   align: 'center'},
                {field: 'createDate',  title: '创建时间', align: 'center'},
                {field: 'lock', title: '操作', width: 200, align: 'center', unresize: true, align: 'center',
                    templet: function (d) {
                        return '<a href="javascript:;" class="font-primary" lay-event="update">修改</a>'
                            + '<a href="javascript:;" class="font-primary" lay-event="del">删除</a>'
                            + '<a href="javascript:;" class="font-primary" lay-event="menu">分配权限</a>';
                    }
                }
            ]]
             ,page:true
        });

        // 检索
        $("#search").click(function () {
            reloadTable(tableObj);
        });
 		// 添加
        $("#role_add").click(function () {
            showDialog({
                title: '新建角色'
                , template: 'desc.html'
                , tableObj: tableObj
                ,yes: function(index, layero){
						layui.form.on('submit(*)', function (data) {
                                    $.ajax({
								    		url:context+ '/api/sysrole',
								    		type: 'POST',
						                    async: false,
						                    dataType: 'json',
						                    contentType: "application/json;charset-UTF-8",
										    data:JSON.stringify(data.field),
										    success: function (result) {
										        if (result.success) {
										            layer.msg("添加成功");
										         	layer.close(index);
										         	reloadTable(tableObj);
										        }else{
										            layer.msg(result.msg);
										        }
										    },
										    error: function () {
										        layer.msg("添加异常");
										    }
										})
                                });

                                //表单验证
                                layui.form.validate();//默认form名称为add_form（可不填写）
                }

            })
        });

        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，mainList是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'update') {
                showDialog({
                title: '编辑角色'
                , template: 'edit'
                , tableObj: tableObj
                , htmlData: data
                ,yes: function(index, layero){
						layui.form.on('submit(*)', function (data) {

                                    $.ajax({
								    		url: context+'/api/sysrole',
								    		type: 'PATCH',
						                    async: false,
						                    dataType: 'json',
						                    contentType: "application/json;charset-UTF-8",
										    data:JSON.stringify(data.field),
										    success: function (result) {
										        if (result.success) {
										            layer.msg("修改成功");
										         	layer.close(index);
										         	reloadTable(tableObj);
										        }else{
                                                    layer.msg(result.msg);
										        }
										    },
										    error: function () {
										        layer.msg("修改异常");
										    }
										})
                                });

                                //表单验证
                                layui.form.validate();//默认form名称为add_form（可不填写）
                }

            })
            }
            // 删除
            if (layEvent === 'del') {
                layer.confirm('确认删除此帐号？', {
                    icon: 7,
                    title: "提示",
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    $.ajax({
                        url: context+'/api/sysrole/'+data.role_id,
                        type:"DELETE",
                        success: function (result) {
                            if (result.success && result.data > 0) {
                                layer.msg("删除成功");
                                reloadTable(tableObj);
                            }else{
                                layer.msg(result.msg);
                            }
                        },
                        error: function () {
                            layer.msg("删除异常");
                        }
                    })
                }, function () {
                    layer.closeAll();
                });
            }

              // 分配权限
            if (layEvent === 'menu') {
                  showDialog({
                title: '分配权限--'+data.roleName
                , template: 'menu'
                , tableObj: tableObj
                , htmlData: data
                ,yes: function(index, layero){

                          var roleId = data.role_id;
                          var oCks = xtree3.GetAllCheckBox(); //这是方法
                           var _index = 0;
                          var paramArr = new Array();
                            for (var i = 0; i < oCks.length; i++) {

                                if (oCks[i].checked&&oCks[i].value!='on') {
                                    var param = {};
                                    param.permissionId = oCks[i].value;
                                    param.roleId = roleId;
                                    paramArr[_index] = param;
                                    _index++;
                                }

                            }
                        layui.form.on('submit(*)', function (data) {

                                    $.ajax({
                                            url: context+'/api/sysrole/role-permission',
                                            type: 'PATCH',
                                            async: false,
                                            dataType: 'json',
                                            contentType: "application/json;charset-UTF-8",
                                            data:JSON.stringify(paramArr),
                                            success: function (result) {
                                                if (result.success) {
                                                    layer.msg("分配权限成功");
                                                    layer.close(index);
                                                    reloadTable(tableObj);
                                                }else{
                                                    layer.msg(result.msg);
                                                }
                                            },
                                            error: function () {
                                                layer.msg("分配权限异常");
                                            }
                                        })
                               });

                                //表单验证
                                layui.form.validate();//默认form名称为add_form（可不填写）
                }

            })
        }

        });

    });
</script>
